define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'planlist/index' + location.search,
                    import_url: 'plan/import',
                    table: 'plan',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id',visible:false, title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'admin_id', title: __('添加人'),searchList: $.getJSON("auth/admin/searchList"), operate: '=',formatter: function (value,row) {
                                 return row.admin_text;
                            }},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},

                        {field: 'g_id',visible:false, title: __('g_id'),searchList: $.getJSON("plan/searchList?status=2"),operate: '=',formatter: function (value,row) {
                                return row.g_id_text;
                            }
                        },
                        {field: 'p_id',visible:false, title: __('p_id'),searchList: $.getJSON("plan/searchList?status=0"),operate: '=',formatter: function (value,row) {
                                return row.p_id_text;
                            }
                        },
                        {field: 'project_id', title: __('Project_id'),searchList: $.getJSON("project/searchList"),operate: 'FIND_IN_SET',formatter: function (value,row) {
                                 if(row.project_id_text.length > 20){
                                    return row.project_id_text.toString().substr(0, 20) + '...';
                                }else{
                                    return row.project_id_text;
                                }
                            }
                        },

                        {field: 'startime',visible:false, title: __('Startime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'endtime',visible:false, title: __('Endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'remarks', title: __('Remarks'), operate: 'LIKE',
                        formatter: function(value,row){
                            if(value.length > 10){
                                return row.remarks_text.toString().substr(0, 10) + '...';
                            }else{
                                return row.remarks_text;
                            }
                        }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'),
                            buttons: [
                                {name: 'detail', text: __('详情'), title: __('详情'), extend: 'data-area= \'["70%", "60%"]\'', icon: 'fa fa-list', classname: 'btn btn-xs btn-success btn-dialog', url: 'plan/detail'},
                            ],
                            table: table, events: Table.api.events.operate,
                            formatter: function(value,row,index){
                                var that = $.extend({},this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit",null);
                                $(table).data("operate-del",null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that,value,row,index);
                            }
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
